package com.maven;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class App {
	
	 private static final String MATCH_PATTERN = "^(.*?).txt$";
	 
	public static List<String> readFileInCollection()
			throws FileNotFoundException {
		Scanner s = new Scanner(new File("string.txt"));
		List<String> list = new ArrayList<String>();
		while (s.hasNext()) {
			list.add(s.nextLine());
		}
		s.close();
		return list;
	}
	
	public static List<String> readFileInCollection2()
			throws FileNotFoundException {
		Scanner s = new Scanner(new File("string.txt"));
		List<String> list2 = new ArrayList<String>();
		while (s.hasNext()) {
			list2.add(s.next());
		}
		s.close();
		return list2;
	}
	private static void writePageTitles(List<String> titlesList, String keyword) {
		try {
			File file = new File(keyword + ".txt");
			if (!file.exists()) {
				file.createNewFile();
			}
			FileWriter fw = new FileWriter(file.getAbsoluteFile());
			BufferedWriter bw = new BufferedWriter(fw);
			for (String title : titlesList) {
				bw.write(title);
				bw.newLine();
			}
			bw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	 public static boolean isStringMatchesMask(String string, String mask) {
	        Pattern pattern = Pattern.compile(mask);
	        Matcher matcher = pattern.matcher(string);
	        return matcher.matches();
	    }
	 
	public static void main(String[] args) throws FileNotFoundException,InterruptedException {

		WebDriver driver = new FirefoxDriver();
		List<String> strings = readFileInCollection();

		for (String queryFromFile : strings) {
			driver.get("http://www.google.com");
			WebElement element = driver.findElement(By.name("q"));
			element.sendKeys(queryFromFile);
			element.submit();
			List<String> pageTitlesList = new ArrayList<String>();
			for (int query = 1; query < 2; query++) {
				Thread.sleep(4000);
				
				WebElement title = driver.findElement(By.xpath("//*[@id=\"rso\"]/li[" + query + "]/div/h3/a"));
				
			//	if ( title.isSelected() && isStringMatchesMask(title.getText(), MATCH_PATTERN)){
					
					pageTitlesList.add(title.getText());
	//			}
			
			}

			writePageTitles(pageTitlesList, queryFromFile);
			
			Scanner s = new Scanner(new File(queryFromFile+ ".txt"));
			List<String>resultsAfterCompare= new ArrayList<String>();
			while (s.hasNext()) {
				resultsAfterCompare.add(s.next());
			}
			s.close();

			System.out.println(resultsAfterCompare);

			/*
			 * Iterator i = readFileInCollection2().iterator(); Iterator j =
			 * resultsAfterCompare.iterator(); String value = (String) i.next();
			 * for (String str : readFileInCollection2()) {
			 * 
			 * }
			 */
			List<String> similar = new ArrayList<String>(
					readFileInCollection2());

			if (similar.retainAll(resultsAfterCompare)) {
				System.out.println(similar);
				System.out.println(similar.indexOf(resultsAfterCompare));
			}

			driver.close();

		}

	}
}